package com.cg.cn.p1_Demo;

import java.util.Random;
import java.util.Scanner;

/*
    2.我们键盘录入一个[1,100]之间的数字,让程序使用Random产生数字猜测,看看程序多少次可以猜对?
 */
public class Demo05BootGuessNumberTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入1-100之间的数字：");
        int target = sc.nextInt();

        Random r = new Random();
        int low = 1, high = 100, count = 0;

        while (true) {
            int guess = r.nextInt(high - low + 1) + low;
            count++;
            System.out.println("程序猜测：" + guess);

            if (guess > target) {
                System.out.println("大了");
                high = guess - 1;
            } else if (guess < target) {
                System.out.println("小了");
                low = guess + 1;
            } else {
                System.out.println("猜中了！用了" + count + "次");
                break;
            }
        }
    }
}

